package com.example.workassignback.Controller;

import com.example.workassignback.POJO.Patient;
import com.example.workassignback.annotation.CurrentUser;
import com.example.workassignback.serviceImpl.service.PatientService;
import com.example.workassignback.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

@Controller
@CrossOrigin
@ResponseBody
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    @PostMapping("/report")
    public Result patientReport(@CurrentUser String id) {
        return patientService.patientReport(Integer.valueOf(id));
    }

    @GetMapping("/getProcess")
    public Result patientGetProcess(@CurrentUser String id) {
        return patientService.patientGetProcess(Integer.valueOf(id));
    }

    @GetMapping("/getHistory")
    public Result patientGetHistory(@CurrentUser String id) {
        return patientService.patientGetHistory(Integer.valueOf(id));
    }

    //涉及DateTime
    @PostMapping("/getHistoryByDate")
    public Result patientGetHistoryByDate(@CurrentUser String id, @RequestBody Map<String, ?> map) {
        String startTimeStr = (String) map.get("startTime");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
        LocalDateTime startTime = LocalDateTime.parse(startTimeStr, formatter);
        return patientService.patientGetHistoryByDate(Integer.valueOf(id), startTime);
    }

    @GetMapping("/getInfo")
    public Result patientGetInfo(@CurrentUser String id) {
        return patientService.patientGetInfo(Integer.valueOf(id));
    }

    @PostMapping("/modifyInfo")
    public Result patientModifyInfo(@CurrentUser String id,@RequestBody Map<String, ?> map) {
        String password = (String) map.get("password");
        String phone = (String) map.get("phone");
        String homeAddress = (String) map.get("homeAddress");
        String dateString = (String) map.get("birthday");
        String contactPerson = (String) map.get("contactPerson");
        String contactPhone = (String) map.get("contactPhone");
        String relation = (String) map.get("relation");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/M/d");
        Date birthday;
        try {
            birthday = formatter.parse(dateString);
            System.out.println("Date: " + birthday);
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.fail("参数异常");
        }

        Patient patient = new Patient();
        patient.setBirthday(birthday);
        patient.setPhone(phone);
        patient.setPassword(password);
        patient.setHomeAddress(homeAddress);
        patient.setContactPerson(contactPerson);
        patient.setContactPhone(contactPhone);
        patient.setRelation(relation);
        patient.setUId(Integer.valueOf(id));
        return patientService.patientModifyInfo(patient);
    }

}
